package lhc.alg.top100;

/**
 * description: https://leetcode.cn/problems/reverse-words-in-a-string-iii/
 *  反转字符串中的单词
 * author: hongchen.liao
 * date:  2022/9/13
 */
public class _557_ {
    class Solution {
        public String reverseWords(String s) {
            StringBuffer ret = new StringBuffer();
            int len = s.length();
            int i = 0;
            while(i < len){
                int start = i;
                while(i < len && s.charAt(i) != ' '){
                    i++;
                }
                for(int p = start; p < i; p++){
                    ret.append(s.charAt(start + i - 1 - p));
                }
                while(i < len && s.charAt(i) == ' '){
                    i++;
                    ret.append(' ');
                }
            }
            return ret.toString();
        }
    }

}
